package demo


import java.text.SimpleDateFormat
/**
 * @Program: ycsy
 * @Description: 各机台合格率推移表
 * @Author: Ivan
 */
class MachinequalifiedController {

    def dynamicQueryService
    def commonSqlService
    def exportFileService

    def machinequalified() {
        request.setCharacterEncoding("UTF-8")
        response.setContentType("text/html;charset=UTF-8")
        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd')
        def startTime = params.startTime ? sdf.parse(params.startTime) : sdf.parse(sdf.format(new Date().clearTime()))
        def endTime = params.endTime ? sdf.parse(params.endTime) : sdf.parse(sdf.format(new Date().clearTime()))
        params.startTime = sdf.format(startTime)
        params.endTime = sdf.format(endTime)
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        params.startno = (pageSize * (pageCurrent - 1)) + 1
        params.endno = pageSize * pageCurrent
        def pageSizeList = [10, 20, 50, 100]
        def tableHeadList = []
        def reportList = []
        def totalCount = 0
        def eqpNumList = []
        def sysQueryName
        if ('Excel'.equals(params.EXPORT_CONTENT_NAME)) {
            eqpNumList = commonSqlService.eqpNumList(params)
            params.SYS_QUERY_NAME = 'machinequalifiedList'
            reportList = dynamicQueryService.queryForList(params)
            sysQueryName = params.SYS_QUERY_NAME
            if (reportList.size() > 0) {
                tableHeadList = reportList[0].keySet()
            }
            totalCount = reportList.size()
        }
        [
                pageSize      : pageSize,
                totalCount    : totalCount,
                pageSizeList  : pageSizeList,
                startTime     : params.startTime ?: '',
                endTime       : params.endTime ?: '',
                eqpNumList    : eqpNumList,
                reportList    : reportList,
                tableHeadList : tableHeadList,
                SYS_QUERY_NAME: sysQueryName,
                totalCount    : totalCount

        ]
    }

    def expExcel() {
        params.startno = 1
        params.endno = 99999999999999999
        def eqpNumList = commonSqlService.eqpNumList(params)
        params.SYS_QUERY_NAME = 'machinequalifiedList'
        def reportList = dynamicQueryService.queryForList(params)
        def tableHeadList = []
        if (reportList.size() > 0) {
            tableHeadList = reportList[0].keySet()
        }
        String filename = new String("repairdrawingList.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8")
        def result = g.render(template: "exp${params.EXPORT_CONTENT_NAME}", model: [eqpNumList: eqpNumList, reportList: reportList, tableHeadList: tableHeadList, startno: params.startno ?: '', endno: params.endno ?: ''])
        exportFileService.exportExcelbyWeb(response, "${params.SYS_QUERY_NAME}", result)
    }
}